// pages/order/tags.js
let app = getApp();
let API = require('../../../api.js').API;
let util = require('../../../utils/util');
let req = require('../../../utils/request').Request;

Page({

  /**
   * 页面的初始数据
   */
  data: {

  },
  /**
   * 添加新的Tag 
   */
  addTag: function () {
    let that=this;
    wx.showModal({
      title: 'DIY your Tag',
      confirmText:'确定',
      cancelText:'下一次',
      editable:true,
      placeholderText:'不超过45个字符',
      success(res) {
        if (res.confirm) {
          console.log(res);
          req.postForm(API.addTag+'?token='+app.globalData.token+'&value='+res.content)
          .then(function(res){
            console.log(res);
            if(res.code==0){
              let tag=res.dataMap.tag;
              that.data.tags.push(tag);
              that.setData({
                tags:that.data.tags
              });
            }
          }).catch(function(res){
            console.log(res);
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },

  /**
   * 监听checkbox
   */
  TagsChange: function (event) {
    console.log(event);
    let order = this.data.order;
    let array = event.detail.value;
    console.log(array);
    this.setData({
      checkedMore: false
    });
    if (array.length > 2) {
      this.setData({
        checkedMore: true
      });
    }
    order.tags = array;
  },

  /**
   * 提交订单
   * @param {*} options 
   */
  submit: function () {
    let order = this.data.order;
    let token = app.globalData.token;
    //console.log(JSON.stringify({order:order}));

    wx.request({
      url: API.createOrder + '?token=' + token,
      method: 'POST',
      header: {
        'content-type': 'application/json'
      },
      dataType: 'json',
      data: JSON.stringify(order),
      success: (res) => {
        console.log(res);
        util.msg(res.data.msg, wx.navigateBack({
          delta: 3
        }));
      }
    })
  },

  /**
   * 刷新
   */
  refresh: function () {
    let that = this;
    req.get(API.getTags + '?token=' + app.globalData.token)
      .then(function (res) {
        if (res.code == 0) {
          that.setData({
            tags: res.dataMap.tags
          });
        }
      }).catch(function (res) {
        console.log(res);
      })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    const eventChannel = this.getOpenerEventChannel();
    eventChannel.on('sendOrder', function (data) {
      that.setData({
        order: data.order,
        checkedMore: false
      });
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.refresh();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})